using Engine.Graphics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace Scenario_Editor.Tools
{
    internal class AddinManager
    {
        #region Private Fields

        private List<string> addinNames;
        private List<Addins> addins;

        #endregion Private Fields

        #region Public Constructors

        public AddinManager(string addinFolder)
        {
            LoadAddins(addinFolder);
        }

        #endregion Public Constructors

        #region Internal Properties

        internal List<Addins> Addins
        {
            get { return addins; }
        }

        #endregion Internal Properties

        #region Public Methods

        public void LoadAddins(string addinFolder)
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(addinFolder);
            string[] files = System.IO.Directory.GetFiles(dir.FullName, "*.dll");
            this.addins = new List<Addins>();
            this.addinNames = new List<string>();
            int number = 0;

            foreach (string file in files)
            {
                try
                {
                    Addins addin = new Addins();
                    addin.DLL = Assembly.LoadFrom(file);

                    Type[] mytypes = addin.DLL.GetExportedTypes();
                    for (int types = 0; types < mytypes.Length; types++)
                    {
                        if (mytypes[types].Name == "streetTool")
                        {
                            addin.addinFile = Activator.CreateInstance(mytypes[types]);
                            addin.GetMethods(mytypes[types]);

                            this.Addins.Add(addin);
                            this.addinNames.Add(System.IO.Path.GetFileNameWithoutExtension(file));

                            number++;
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show(file + " is not a valid Plugin!\n" + e.Message);
                }
            }
        }

        #endregion Public Methods

        #region Internal Methods

        internal void Dispose()
        {
            for (int i = 0; i < this.addins.Count; i++)
            {
                this.addins[i].OnSceneryEditorCloseing();
            }
        }

        internal int GetPluginID(string pluginName)
        {
            return this.addinNames.IndexOf(pluginName);
        }

        internal string GetPluginName(int addinId)
        {
            return this.addinNames[addinId];
        }

        internal List<string> GetPluginNames()
        {
            return this.addinNames;
        }

        internal object LoadCrossroadProperties(int usedPlugin, BinaryReader br)
        {
            if (usedPlugin > -1)
            {
                return this.addins[usedPlugin].LoadCrossroadProperties(br);
            }
            throw new Exception("crossroads properties cannot be loaded. The given Id is less than zero!");
        }

        internal void MaterialManagerChanged(object sender, EventArgs e)
        {
            foreach (var addin in this.addins)
            {
                addin.GiveTexturesPaths((MaterialManager)sender);
            }
        }

        internal void OnTerrainChange(object iTerrain, EventArgs e)
        {
            for (int i = 0; i < this.addins.Count; i++)
            {
                this.addins[i].OnTerrainChange((ITerrain)iTerrain);
            }
        }

        #endregion Internal Methods
    }
}